package com.wsg.lightaccounting.PopupWindow;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wsg.lightaccounting.Activity.AccountActivity;
import com.wsg.lightaccounting.Adapter.AccountBookListAdapter;
import com.wsg.lightaccounting.Adapter.AccountListAdapter;
import com.wsg.lightaccounting.Fragment.RecordFragment;
import com.wsg.lightaccounting.Model.RecordManager;
import com.wsg.lightaccounting.R;

/**
 * Created by dell-pc on 2016/11/20.
 */

public class AccountBookPopupWindow extends PopupWindow{
    private ListView list;
    private View mMenuView;
    public AccountBookPopupWindow(final Activity context, int w, int h){
        super(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        mMenuView = inflater.inflate(R.layout.select_picpopupwindow_layout, null);
        list = (ListView) mMenuView.findViewById(R.id.list);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        list.setAdapter(new AccountBookListAdapter(context));
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView b= (TextView) context.findViewById(R.id.account_book_record);
                b.setText(RecordManager.accountBookses.get(position).getName());
                RecordFragment.account_book_id = id;
                Intent intent = new Intent();
                intent.setAction(RecordFragment.UPDATEUI_RECORD_FRAGMENT);
                context.sendBroadcast(intent);
                dismiss();
            }
        });
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(h);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
       // this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });

    }
}
